

#ifndef WCI_h__
#define WCI_h__

#include <stdint.h>

typedef struct WCIStruct WCI;

#define TimeUnit_Ms		(unsigned int)(13.56 * 1000)
#define TimeUnit_MsDeci		(TimeUnit_Ms / 10)
#define WaitTime_Ms(ms, deci) ((ms) * TimeUnit_Ms + (deci) * TimeUnit_MsDeci)

#define WCI_ForceMark	(1 << 15)

enum {
	WCI_Mode_A,
	WCI_Mode_B,
};

enum {
	WCI_Cfg_Short,
	WCI_Cfg_Anti,
	WCI_Cfg_Sel,
	WCI_Cfg_HaltA,
	WCI_Cfg_RATS,
	WCI_Cfg_ExchangeA,
	WCI_Cfg_RfOn,
	WCI_Cfg_RfOff,
};

enum {
	WCI_S_OK = 0,
	WCI_S_Timeout = 1,
	WCI_S_Coll = 2,
	WCI_S_Hardware = 3,
	WCI_S_Unknow = 4,
	WCI_S_OverRun = 5,
	WCI_S_Crc = 6,
	WCI_S_Parity = 7,
	WCI_S_Protocol = 8,
};

enum {
	WCI_Ctrl_Auth = 0,
};


enum {
	WCI_Cmd_Exchagne,
	WCI_Cmd_Authen,
	WCI_Cmd_LoadKey,
};


struct WciTimeTag {
	unsigned int waitTime;
	unsigned int guardTime;
};

typedef struct WciBitsTag {
	uint8_t lastTx;
	uint8_t lastRx;
}WciBits_T;

typedef struct WciTimeTag WciTime_T;

void WCI_Delay_Fc(unsigned int fc);

void WCI_Register(WCI* wci);

void WCI_Init(void);
void WCI_SetRf(WCI* wci, uint8_t state);
void WCI_InitTime(WciTime_T * time, unsigned int waitTime, unsigned int guardTime);
int WCI_SetMode(WCI* wci, int mode);
int WCI_Config(WCI* wci, int config);
int WCI_ClearFifo(WCI * wci);
int WCI_WriteFifo(WCI * wci, uint8_t buf[], uint16_t len);
int WCI_ReadFifo(WCI * wci, uint8_t buf[], uint16_t len);
int WCI_FifoSpace(WCI * wci);
int WCI_FifoLength(WCI * wci);
int WCI_Command(WCI * wci, uint8_t cmd, WciTime_T * time, void * param);
int WCI_Exchange(WCI * wci, 
	unsigned char * tx, unsigned tlen, 
	unsigned char * rx, unsigned * rlen, unsigned rmax, 
	unsigned waitTime, unsigned int guardTime);
int WCI_ExchangeBit(WCI* wci, unsigned char* tx, unsigned int tlen, unsigned int tbits,
	unsigned char* rx, unsigned int* rlen, unsigned int *rbits, unsigned int rmax,
	unsigned int wTime, unsigned int gTime) ;

int WCI_Support(WCI* wci);
int WCI_Control(WCI* wci, unsigned char ctrl, 
	unsigned char *pin, unsigned int ilen, unsigned char* pout, unsigned int *olen);
#endif // WCI_h__

